package com.suny.sf_server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suny.FarmStocks.dto.FarmStocksQueryDTO;
import com.suny.sf_server.service.FarmStocksService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.suny.FarmStocks.dto.FarmStocksAddDTO;
import com.suny.FarmStocks.vo.FarmStocksVO;
import com.suny.exception.BusinessException;
import com.suny.result.PageDTO;
import com.suny.result.PageVO;
import com.suny.result.R;

import java.util.List;

@Slf4j
@Tag(name = "存栏信息", description = "存栏信息相关接口")
@RestController
@RequestMapping("/farmStocks")
public class FarmStocksController {
    @Resource
    private FarmStocksService farmStocksService;
    
    @Operation(summary = "新增存栏")
    @PostMapping("/add")
    public R add(@Validated @RequestBody FarmStocksAddDTO dto) {
        try {
            farmStocksService.add(dto);
            return R.OK("新增存栏成功");
        } catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        }
    }
    
    @Operation(summary = "出栏")
    @PutMapping("/{id}")
    public R outStock(@Parameter(description = "存栏ID") @PathVariable Integer id) {
        try {
            farmStocksService.outStock(id);
            return R.OK("出栏成功");
        } catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        }
    }
    
//    @Operation(summary = "获取存栏列表")
//    @PostMapping("/list")
//    public R getList(@Validated @RequestBody PageDTO pageDTO) {
//        try {
//            Page<FarmStocksVO> page = farmStocksService.getList(pageDTO);
//
//            // 构造返回的分页数据
//            PageVO<FarmStocksVO> result = new PageVO<>();
//            result.setCurrent(page.getCurrent());
//            result.setSize(page.getSize());
//            result.setTotal(page.getTotal());
//            result.setPages(page.getPages());
//            result.setRecords(page.getRecords());
//            return R.OK(result);
//        } catch (BusinessException e) {
//            return R.FAIL(e.getMessage());
//        }
//    }
    
    @Operation(summary = "获取牧场存栏列表")
    @GetMapping("/{farmId}")
    public R getListByFarmId(@Parameter(description = "圈舍ID") @PathVariable Integer farmId) {
        try {
            List<FarmStocksVO> list = farmStocksService.getListByFarmId(farmId);
            return R.OK(list);
        } catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        }
    }
    
    @Operation(summary = "统计牧场未出栏数量")
    @GetMapping("/farm/{farmId}")
    public R countFarmLivestock(@Parameter(description = "圈舍ID") @PathVariable Integer farmId) {
        try {
            Integer count = farmStocksService.countFarmLivestock(farmId);
            return R.OK(count);
        } catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        }
    }
    
    @Operation(summary = "统计圈舍未出栏数量")
    @GetMapping("/pen/{penId}")
    public R countPenLivestock(@Parameter(description = "圈舍ID") @PathVariable Integer penId) {
        try {
            Integer count = farmStocksService.countPenLivestock(penId);
            return R.OK(count);
        } catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        }
    }
    
    @Operation(summary = "统计围栏未出栏数量")
    @GetMapping("/fence/{fenceId}")
    public R countFenceLivestock(@Parameter(description = "围栏ID") @PathVariable Integer fenceId) {
        try {
            Integer count = farmStocksService.countFenceLivestock(fenceId);
            return R.OK(count);
        } catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        }
    }

    @Operation(summary = "获取存栏列表（带条件）")
    @PostMapping("/list")
    public R getListByCondition(@Validated @RequestBody FarmStocksQueryDTO query) {
        try {
            Page<FarmStocksVO> page = farmStocksService.getListByCondition(query);

            // 构造返回的分页数据
            PageVO<FarmStocksVO> result = new PageVO<>();
            result.setCurrent(page.getCurrent());
            result.setSize(page.getSize());
            result.setTotal(page.getTotal());
            result.setPages(page.getPages());
            result.setRecords(page.getRecords());
            return R.OK(result);
        }
        catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        }
    }
}
